package com.banmao.algorithm.No101To200.No199;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

public class Solution1 {

    public List<Integer> rightSideView(TreeNode root) {

        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        ArrayList<Integer> result = new ArrayList<>();

        if(root == null) {
            return result;
        }

        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            int index = 0;

            while (index < size) {
                index++;
                TreeNode poll = queue.poll();
                if(poll.left != null) {
                    queue.add(poll.left);
                }
                if(poll.right != null) {
                    queue.add(poll.right);
                }

                if(index == size) {
                    result.add(poll.val);
                }
            }
        }

        return result;
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
